<?php
include_once('Publication.php');
include_once('Organization.php');
include_once('Author.php');
include_once('calcHindex.php');
include_once('mult.php');

if(isset($_GET['u1'])) {
   
    
    $uIDArray = array();
    $uID = 'u1';
    $uIDcount = 1;
    
    while (isset($_GET[$uID])) 
    {
        array_push($uIDArray, $_GET[$uID]);
        $uIDcount++;
        $uID = "u" . $uIDcount;
        
    }
    
    $domainID = $_GET['domainID'];
    $subDomainID = $_GET['subDomainID'];
    $yearStart = intval($_GET['yearStart']);
    $yearEnd = intval($_GET['yearEnd']);
ini_set('max_execution_time', 300);

$arrayOfUniv = array();

foreach ($uIDArray as $u) 
{
	$arrayOfUniv[$u] = array();
	
}


$totalPoints = ($yearEnd - $yearStart + 1) * ($uIDcount-1);
$calcHIdx = intval($_GET['calcHIdx']);
$batchMax = ($calcHIdx) ? 18 : 45;



$results = array();
$yearArray = array();

$currentUniversity = 0;

$batch = array();
$reqSizePerUniv = ($yearEnd - $yearStart + 1);
$batchSize = 0;
$curYear = $yearStart;
$initialUniv = 0;
$initialYear = $yearStart;

$schoolArray = array();



for ($i=1; $i<=$totalPoints; $i++) 
{
   
	
	
    $IDofUniv = $uIDArray[intval($currentUniversity)];
    //$fundingArray = getFunding($u);
  
        $org = new Organization;
        $author = new Author;
        $pub = new Publication;
        
        if ($domainID !=0) 
        {
            $org->domainID = $domainID;
            $author->domainID = $domainID;
            $pub->domainID = $domainID;
        }
   
        
        if ($subDomainID !=0) 
        {
            $org->subDomainID = $subDomainID;
            $author->subDomainID = $subDomainID;
            $pub->subDomainID = $subDomainID;
        }
        
        $org->orgID =  $IDofUniv;
        $author->orgID = $IDofUniv; 
        $pub->orgID = $IDofUniv; 
        
        
        $org->yearStart=$curYear;
        $org->yearEnd=$curYear;

        $org->startIdx=1;
        $org->endIdx=1;
        
        $author->yearStart=$curYear;
        $author->yearEnd=$curYear;
        
        $pub->yearStart=$curYear;
        $pub->yearEnd=$curYear;

        $pub->orderBy = "CitationCount";
        
        
        array_push($batch, $org->getRequest());
        array_push($batch, $author->getRequest());
        
        if ($calcHIdx)
        {
	        $pub->startIdx=1;
	        $pub->endIdx=100;
	        array_push($batch, $pub->getRequest());
	       	$pub->startIdx=101;
	       	$pub->endIdx=200;
	        array_push($batch, $pub->getRequest());
	       	$pub->startIdx=201;
	       	$pub->endIdx=300;
	        array_push($batch, $pub->getRequest());
	        
	        $publicationArray = array();
        }
    
       
 
        if ($batchSize == $batchMax || $i == $totalPoints) 
        {
        	$r = multiRequest($batch);
        	

		
        	//echo json_encode($r);
			
			$objectCount = 0;
			//$point = new schoolInfo;
			foreach ($r as $response) 
			{
			$json = json_decode($response);
				
  				if ($objectCount == 0)
  				{
  					$point = new schoolInfo;
  					
  		
  					
  					$point->totalPub = $json->d->Organization->Result[0]->PublicationCount;
  					$point->citations= $json->d->Organization->Result[0]->CitationCount;
  					
  					$objectCount++;
  				}
				elseif ($objectCount == 1)
  				{
  					//echo $response;
  					$point->authorCount= $json->d->Author->TotalItem;
  					$objectCount++;
  					
  					
  					if (!$calcHIdx)
  					{
  						$point->hIdx = 0;
  						$objectCount = 0;
  						$point->normalizedAuthor = 0;
  					
    					array_push($arrayOfUniv[$uIDArray[$initialUniv]], $point);
    				
    		
    				
  						if ($initialYear == $yearEnd) 
  						{
							$initialYear = $yearStart;
							$initialUniv++;
						}
						else 
						{
							$initialYear++;
						}						
  					}
  				}
				elseif ($objectCount == 2)
  				{
  					$publicationArray = getCitArray($json->d->Publication->Result);
  					
  					$objectCount++;
  					
  		
  				}
			elseif ($objectCount == 3 && $calcHIdx)
  				{
  					
  					$tempArray = getCitArray($json->d->Publication->Result);
  					
  					
  					$publicationArray = array_merge($publicationArray, $tempArray);
  					
  					
  					//$publicationArray + $json->d->Publication->Result;
  					$objectCount++;
  				}
  				else 
  				{
  					$tempArray = getCitArray($json->d->Publication->Result);
  					$publicationArray = array_merge($publicationArray, $tempArray);
  					
  					
  					$point->hIdx = printCitations($publicationArray);
  					//$point->hIdx = 0;
  					$objectCount = 0;
  					$point->normalizedAuthor = 0;
  					//$point->funding = 
  					//echo $response;
  				
    				array_push($arrayOfUniv[$uIDArray[$initialUniv]], $point);
    				
    				
    				$publicationArray = array();
    				
  					if ($initialYear == $yearEnd) 
  					{
						$initialYear = $yearStart;
						$initialUniv++;
					}
					else 
					{
						$initialYear++;
						
					}
					//array_push($schoolArray, $arrayOfUniv[0]);
  				}
    	
    		//echo $response;
    	
    		
		
			
			}
       		if ($i != $totalPoints)
       		{
				$batchSize = 0;
				//clear the batch;
				$batch = array();
			
				if ($calcHIdx)
				{
					sleep(30);
				}
				else 
				{
					sleep(32);
				}
       		}
        }
        else {
        	$batchSize++;
        }
        
		if ($curYear < $yearEnd) {
        	$curYear++;
        }
        else 
        {
        	$curYear = $yearStart;
        	$currentUniversity++;
        	//$initialUniv = $currentUniversity;
        }
        
        
        
        /*
        $numAuthors = intval($author->getAuthorCount());
        if ($numAuthors == 0) 
        {
            $numAuthors = 1;
        }
   
       $firstSchool = new schoolInfo;
   
        $firstSchool->normalizedAuthor = 0;
        $firstSchool->authorCount = $numAuthors;
        $firstSchool->totalPub = $org->getCitationCount();
        $firstSchool->hIdx = 0; //calcHindex($u, $i, $i, $domainID, $subDomainID);
        if (array_key_exists((string) $i, $fundingArray)) 
        {
            $firstSchool->funding = $fundingArray[(string) $i];
        }
        else 
        {
            $firstSchool->funding = 10000000000000;
        }*/
        
        
        //array_push($schoolArray, $firstSchool);
 
   
    
}
    


foreach ($arrayOfUniv as $u) 
{
    array_push($schoolArray, $u);
}


for ($i=$yearStart; $i<=$yearEnd; $i++)
{

    array_push($yearArray, $i);
    
    
}

    $jsonObj = new jsonObj;
    $jsonObj->schools = $schoolArray; 
    
    $jsonObj->yearArray = $yearArray;


echo json_encode($jsonObj);


}

function processBatch($batch, $initialUniv, $curYear, $yearEnd )
{
	$r = multiRequest($batch);
	
	foreach ($r as $response) 
	{
  		
    	$json = json_decode($response);
    	$point = new schoolInfo;
    	$point->normalizedAuthor = 0;
       	$point->authorCount = $json->d->Author->TotalItem;
        $point->totalPub = $json->d->Oganization->Result[0]->PublicationCount;
        $point->hIdx = 0; //calcHindex($u, $i, $i, $domainID, $subDomainID);
  	
		array_push($arrayOfUniv[$uIDArray[$initialUniv]], $point);
		
		if ($curYear = $yearEnd) {
			$curYear = $yearStart;
			$initialUniv++;
		}
}
}

function getFunding($orgID) 
{
 $db = mysql_connect('localhost', 'boughtns_user', 'GodLovesYou!');
    
if (!$db) {
    die('Could not connect: ' . mysql_error());
 }
    
mysql_select_db('boughtns_mas', $db);

    $query = "SELECT year, amount FROM funding where university_id = " . $orgID;
   
    $result = mysql_query($query);
    
    $fundingArray = array();
   
    while ($row = mysql_fetch_assoc($result)) 
    {
        $year = $row['year'];
        $amount = $row['amount'];
            
        $fundingArray[$year] = $amount;     
       
    }
    mysql_close($db);    
    return $fundingArray;
    
}

function getCitArray ($array) 
{
	$len = sizeof($array);
	$citArray = array();
	for ($i=0;$i<$len;$i++) 
	{
	$citArray[$i] = $array[$i]->CitationCount;

	}
	return $citArray;
}

class jsonObj 
{
    public $schools, $yearArray;
}

class schoolInfo
{
    public $normalizedAuthor, $authorCount, $totalPub, $hIdx, $funding, $citations;
}

?>